package cn.jbolt.ec.coupon;


import cn.jbolt.base.JBoltBaseService;


import cn.jbolt.common.model.BecRetailerChargeBonus;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;

public class RetailerCouponService extends JBoltBaseService<BecRetailerChargeBonus> {
    private BecRetailerChargeBonus dao = new BecRetailerChargeBonus().dao();
    @Override
    protected BecRetailerChargeBonus dao() {
        return new BecRetailerChargeBonus().dao();
    }

    public Page<BecRetailerChargeBonus> get_retailer_coupon_page(Integer pageNum, Integer pageSize, String keywords
                                                            , Integer retailer_id, Long on_time){
        Kv conf = Kv.by("table",table());
        conf.setIfNotBlank("keywords",keywords);
        conf.setIfNotNull("retailer_id",retailer_id);
        if ( on_time != null){
            Long now_time = System.currentTimeMillis();
            Long judge_time = now_time + on_time*3600*24*1000;
            conf.setIfNotNull("judge_time",judge_time);
            conf.setIfNotNull("now_time",now_time);
        }

        return paginateBySqlTemplate("ec.mall.manage.retailer.get_retailer_coupon_page",conf,pageNum,pageSize);
    }



    public Ret save(BecRetailerChargeBonus becRetailerChargeBonus,String charge_amount,String value, Date expired){
        if (becRetailerChargeBonus == null || expired == null ||charge_amount ==null || value==null)
            return fail("操作失败");

        Integer charge_amount_int = Integer_by_String(charge_amount);
        Integer value_int = Integer_by_String(value);
        if (charge_amount_int ==null || value_int == null)
            return fail("操作失败");

        becRetailerChargeBonus.setAmount(charge_amount_int);
        becRetailerChargeBonus.setBonus(value_int);
        Long start_time = System.currentTimeMillis();
        becRetailerChargeBonus.setStart(start_time);

        Long expired_time = expired.getTime();
        becRetailerChargeBonus.setExpired(expired_time);

        becRetailerChargeBonus.setStatus(1);

        boolean success = becRetailerChargeBonus.save();
        if (success)
            return success("操作成功");
        return fail("操作失败");
    }



    public Ret update(BecRetailerChargeBonus becRetailerChargeBonus,String charge_amount,String value, Date expired){
        if (becRetailerChargeBonus == null || expired == null ||charge_amount ==null || value==null)
            return fail("操作失败");

        Integer charge_amount_int = Integer_by_String(charge_amount);
        Integer value_int = Integer_by_String(value);
        if (charge_amount_int ==null || value_int == null)
            return fail("操作失败");

        becRetailerChargeBonus.setAmount(charge_amount_int);
        becRetailerChargeBonus.setBonus(value_int);

        Long expired_time = expired.getTime();
        becRetailerChargeBonus.setExpired(expired_time);

        boolean success = becRetailerChargeBonus.update();
        if (success)
            return success("操作成功");
        return fail("操作失败");
    }


    public Ret delete(Integer coupon_id){
        if (coupon_id == null)
            return fail("操作失败");
        BecRetailerChargeBonus becRetailerChargeBonus = findById(coupon_id);
        if (becRetailerChargeBonus == null)
            return fail("没有该优惠");
        boolean success = becRetailerChargeBonus.delete();
        if (success)
            return success("操作成功");
        return fail("操作失败");

    }


    public Integer Integer_by_String(String s){
        Double s_double = Double.parseDouble(s);
        s_double = s_double *100;
        return s_double.intValue();
    }

}
